package ms.core.serv.tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import ms.core.serv.system.Application;
import ms.core.tool.Log4j2Tool;
import ms.core.tool.StreamTool;

public class WhiteIpLoader {
	static String conf = "/config/iptable.lst";
	private static List<String> whiteIps;
	
	public static void init() {
		if (!Application.IP_FILTER || whiteIps!=null)
			return;
		
		whiteIps = new ArrayList<>();
        try {
        	InputStream is = StreamTool.loadFileToStream(TaskLoader.class, conf);
        	InputStreamReader isr = new InputStreamReader(is);
        	BufferedReader br = new BufferedReader(isr);
        	String txt = null;
			while((txt = br.readLine())!=null){
				whiteIps.add(txt.trim());
			}
			br.close();
			isr.close();
			is.close();
		} catch (IOException e) {
			Log4j2Tool.error(WhiteIpLoader.class, e);
		}
	}
	
	public static boolean legalIp(String ip) {
		if (!Application.IP_FILTER)
			return true;
		
		return whiteIps.contains(ip);
	}
}
